package com.example.xiaoyuan.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.xiaoyuan.pojo.Admin;
import com.example.xiaoyuan.service.AdminService;
import com.example.xiaoyuan.utils.MD5;
import com.example.xiaoyuan.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "管理员控制器")
@RestController
@RequestMapping("/sms/adminController")
public class AdminController {

    @Autowired
    private AdminService adminService;

    /**
     * 单个或多个删除管理员
     * @param ids
     * @return
     */
    @DeleteMapping("/deleteAdmin")
    public Result deleteAdmin(@RequestBody List<Integer> ids){

        adminService.removeByIds(ids);
        return Result.ok();
    }

    /**
     * 增加或者修改管理员
     * @param admin
     * @return
     */
    @PostMapping("/saveOrUpdateAdmin")
    public Result saveOrUpdateAdmin(@RequestBody Admin admin){

        Integer id = admin.getId();
        if (id == null || 0 ==id){
            admin.setPassword(MD5.encrypt(admin.getPassword()));
        }

        adminService.saveOrUpdate(admin);
        return Result.ok();
    }

    /**
     * 分页获取管理员信息
     * @param pageNo
     * @param pageSize
     * @param adminName
     * @return
     */
    @GetMapping("/getAllAdmin/{pageNo}/{pageSize}")
    public Result getAllAdmin(
            @PathVariable Integer pageNo,
            @PathVariable Integer pageSize,
            String adminName
    ){
        Page<Admin> adminPage = new Page<Admin>(pageNo, pageSize);

        IPage<Admin> iPage = adminService.getAdminsByOpr(adminPage, adminName);

        return Result.ok(iPage);
    }
}
